package cn.zhj.mvpart.mvp

import android.content.Context
import android.os.Bundle
import android.view.InflateException
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import cn.zhj.mvpart.base.IFragment

open abstract class BaseFragment<out P:IPresenter> : Fragment(), IFragment,IView {

    private val mPresenter : P by lazy { createPresenter() }

    protected var mContext : Context? = null

    protected fun getPresenter() = mPresenter

    abstract fun createPresenter(): P


    override fun onAttach(context: Context) {
        super.onAttach(context)
        mContext = context
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return initView(inflater, container, savedInstanceState)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        mPresenter.onCreate(this)
        initData(savedInstanceState)
    }


    override fun onDestroy() {
        super.onDestroy()
        mPresenter.onDestroy() //释放资源
    }

    override fun onDetach() {
        super.onDetach()
        mContext = null
    }
}